#include "PlayerStateWallSlide.h"
#include "Player.h"

PlayerStateWallSlide::PlayerStateWallSlide(Player* player) : 
PlayerState(player){}

PlayerStateWallSlide::~PlayerStateWallSlide(void){}

void PlayerStateWallSlide::activate()
{
	_slideDirection = _player->getFlip();
	_player->getAnimationMgr().setAnimation("wall_slide");
	_player->resetDash();
	_player->removeFlag(GRAVITY_AFFECTED);
	_resetGravity = true;
}

void PlayerStateWallSlide::deactivate()
{
	if (_resetGravity)
		_player->setFlag(GRAVITY_AFFECTED);
}

int PlayerStateWallSlide::update()
{
	if (_player->damageReceived())
		return PSRV_TAKE_DAMAGE;
	_player->_velocity.y = _player->getWallSlideSpeed() * GameTime::getSingleton()->deltaTime();

	if (_player->getControlState()->isKeyDown(CControls::Key(CTRL_DASH, 0), 0))
		_player->dash();
	else
		_player->resetDash();

	if (_player->getControlState()->isKeyPressed(CControls::Key(CTRL_JUMP, 0), 0))
	{
		_resetGravity = false;
		return PSRV_WALL_JUMP;
	}

	_player->applyVelocity();
	
	if (_player->getMovementStateX() == MSX_NONE)
		return PSRV_FALLING;
	else if (_slideDirection == -1.0f && _player->getMovementStateX() == MSX_RIGHT)
	{
		return PSRV_FALLING;
	}
	else if (_slideDirection == 1.0f && _player->getMovementStateX() == MSX_LEFT)
	{
		return PSRV_FALLING;
	}
	else if (_slideDirection == -1.0f && ((_player->flags() & COLLISION_HIT_LEFT) != COLLISION_HIT_LEFT))
		return PSRV_FALLING;
	else if (_slideDirection == 1.0f && ((_player->flags() & COLLISION_HIT_RIGHT) != COLLISION_HIT_RIGHT))
		return PSRV_FALLING;

	return PSRV_NOTHING;
}